package com.changfang.gold_man.service.Imp;

import com.changfang.gold_man.entity.Task;
import com.changfang.gold_man.mapper.TaskMapper;
import com.changfang.gold_man.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserServiceImpl userServiceimpl;

    //查找所有任务
    @Override
    public List<Task> findAll(){
        return taskMapper.findAll();
    }
//    添加任务
    @Override
    public String addTask(Map<String, String> task) {
        Task task1 = new Task();
        task1.setTask_name(task.get("task_name"));
        task1.setTask_msg(task.get("task_msg"));
        task1.setTask_gold(Float.parseFloat(task.get("task_gold")));
        task1.setTask_status("0");
        task1.setTask_s_time(task.get("task_s_time"));
        task1.setTask_e_time(task.get("task_e_time"));
        task1.setTask_creator_id(task.get("task_creator_id"));
        if (taskMapper.findName(task.get("task_name"))==null){
            return "该任务已经存在";
        }else {
            taskMapper.addTask(task1);
            return "1";
        }
    }
//id查找任务
    @Override
    public Task findId(String task_id) {
        Integer task_id1 = Integer.parseInt(task_id);
        return   taskMapper.findId(task_id1);
    }
//name查找任务
    @Override
    public List<Task> findName(String task_name) {
        return taskMapper.findName(task_name);
    }
//查找待完成任务
    @Override
    public List<Task> findEnableTask() {
        return taskMapper.findEnableTask();
    }
//接受任务
    @Override
    public void receive(String task_id, String user_id) {
        taskMapper.receive(Integer.parseInt(task_id),user_id);
    }
    //完成任务
    @Override
    public String complete(Integer task_id) {
        try {
            Task task = findId(Integer.toString(task_id));
            String creater_id = task.getTask_creator_id();
            String receiver_id = task.getTask_receiver_id();
            Float task_gold = task.getTask_gold();
            String goldStr = Float.toString(task_gold);
            userServiceimpl.updateGold(creater_id,"r"+goldStr);
            userServiceimpl.updateGold(receiver_id,"a"+goldStr);
            taskMapper.complete(task_id,"1");
            return "success";
        } catch (Exception e){
            return "err";
        }
    }
    //查询我接受的任务
    @Override
    public List<Task> findMyReceive(String receiver_id) {
        return taskMapper.findMyReceive(receiver_id);
    }
    //删除任务
    @Override
    public void delete(String task_id) {
        taskMapper.delete(Integer.parseInt(task_id));
    }
    //创建者ID查任务
    @Override
    public List<Task> findMyCreater(String creater_id) {
        return taskMapper.findMyCreater(creater_id);
    }
    //关键字查任务
    @Override
    public List<Task> findKeyStr(String keyStr) {
        String  newKeyStr = "%"+keyStr+"%";
        return taskMapper.findKeyStr(newKeyStr);
    }
}
